#include "event.h"
#include<iostream>
using namespace std;
using namespace Json;

long long int Slot::count = 0;

Slot::Slot(SlotListener* listener,const EventHandler& evh,const bool& once)
{
    this->listener = listener;
    this->flag=false;
    this->evh = evh;
    this->once = once;
    this->id = count++;
}

Slot::Slot(SlotListener* listener, const std::function<void(Json::Value&)>& func, const bool& once) {
    this->listener = listener;
    this->flag = true;
    this->func = func;
    this->once = once;
    this->id = count++;
}

void Slot::dispatch(Json::Value& message)
{
    cout<<"Slot::dispatch"<<message.toStyledString()<<endl;
    if (flag)
    {
        func(message);
    }
    else
    {
        cout<<"Slot::dispatch 2"<<message.toStyledString()<<endl;
        (evh.handler->*evh.func)(message);
    }
    if (once)
        this->remove();
}

bool Slot::operator==(const Slot & slot)
{
    return this->id == slot.id;
}

long long int Slot::getId() const
{
    return this->id;
}

void Slot::remove()
{
    this->listener->removeSlot(this->getId());
}


EventHandler handler(Object* CCObject,SEL_EventFunc func)
{
    EventHandler evh;
    evh.handler = CCObject;
    evh.func = func;
    return evh;
}
